home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-03-03 | 3.0 KB | 117 lines | [UVtx/UVtl] |
- // library routines for the Towers of Hanoi
-
- // GetNumberOfTowers - ask for the number, and return it
- ////////////////////////////////////////////////////
- Function GetNumberOfTowers(n)
- Pen name="GNTpen"
- Clear 53066,64909,65535
- Color 56912,2891,21210
- Up
- Goto -100,0,0
- Print "Number of rings (1..10): ", move
- Down
- Forward 25
- Backward 25
- n = ask(30)
- If (n < 1) | (n > 10)
- Up
- Goto -100,-30,0
- Print "Sorry, must be between 1 and 10!"
- Sound "Oops"
- Halt
- Return n
-
- // SetUp - build the towers, build the ring, init vars
- ///////////////////////////////////////////////
- Function SetUp()
- ringHeight = 15
- // create the N rings
- Repeat i,1,N
- Picture "ring" . i
- Up
- Goto -i*5,0,0
- Down
- Fill 1,random(65000),random(65000),random(65000)
- Repeat 2
- Forward i*10
- Left 90
- Forward ringHeight-1
- Left 90
- Pen name=i, picture="ring" . i // pen i is ring i
- Pen name=""
- Clear
- // a pen for writing the number of rings moved
- nMoved = 0
- Pen name="moved", fontsize=20,fontstyle="Bold",fontname="Times"
- Up
- Goto -width()/2+100, height()/2-40,0
- Color 6360,65535,4549
- Print "moving " . (2^N - 1) . " rings"
- Goto width()/2-100, height()/2-40,0
- Pen name=""
- // draw the towers
- towerBaseY = -height()/2 + 20
- towerTopY = towerBaseY + ringHeight * 11
- Repeat i,1,3
- towerX[i] = (i-2) * 110
- Up
- Goto towerX[i]-3, towerBaseY, 0
- Down
- Fill 1,0,0,0
- Repeat 2
- Forward 6
- Left 90
- Forward towerTopY-towerBaseY
- Left 90
-
- // drop the N ring on tower #1
- nTower[1] = N
- nTower[2] = 0
- nTower[3] = 0
- Repeat i,N,1,-1
- Pen name=i, speed=1000,end="yes"
- Up
- Goto towerX[1], height(),0
- Down
- Sound "Whip",wait
- Goto x(), towerBaseY + (N-i)*ringHeight,-90
- dong(5)
-
-
- // MoveOne - move one ring from one pole to another
- ///////////////////////////////////////////////
- Function MoveOne(ring,from,to)
- If doPause
- ask(1)
- Pen name=ring, speed=speed1
- Down
- Goto towerX[from], towerTopY+50, 0
- Goto towerX[to], y(), 0
- Pause thePause
- Pen speed=speed2
- Goto x(), towerBaseY + nTower[to]*ringHeight,-90
- dong(3)
- nTower[from] = nTower[from] - 1
- nTower[to] = nTower[to] + 1
- Pen name="moved"
- Down
- Color 65535,65535,65535
- Fill 1,65535,65535,65535
- Repeat 2
- Forward 99
- Left 90
- Forward 39
- Left 90
- Color 65535,7106,29616
- nMoved = nMoved+1
- Print nMoved
-
- // dong - bounce the disk and play the metal sound
- /////////////////////////////////////////////
- Function dong(n,a)
- Sound "Pole", nowait
- Pen speed=200
- Repeat a,n,1,-1
- Backward a
- Forward a
-